package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.TaskPointChooseRelativethingBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.greenrobot.event.EventBus;

/**
 * author ：minifly
 * date: 2017/10/30
 * time: 16:57
 * desc:
 */
public class UnitGroupAllPagerFragment5 extends CommonBasePagerFragment implements View.OnClickListener {

    private Button mBtSave;
    public static final int REQUEST_1 = 1003,REQUEST_CODE = 10004;
    private ArrayList<TaskPointChooseRelativethingBean.ListBean> listBean;

    private Map<String,TaskPointChooseRelativethingBean.ListBean> listBeanMap = new HashMap<>();
    private String jobPointUkid;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof UnitGroupAllPagerFragment5) {
            mActivity.setTitle("" + getString(R.string.new_task_step6_title_txt));
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

//        jobPointUkid = getArguments().getString("jobPointUkid");

        showConfirmButton();
        setConfirmBtnText("" + getString(R.string.new_task_save));
        mBtSave = (Button) getConfirmBtn();
        mBtSave.setOnClickListener(this);
        requestHttp();
        EventBus.getDefault().register(this);
    }

    private void initPager() {
        Bundle budle = new Bundle();
        try {
            setData(listBean.size(), 10, "com/wwwarehouse/taskcenter/fragment/job_point/step6/ChooseConnectObjectDetailFragment6", budle, listBean);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }


    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadSuccess(String s, int i) {
        if (REQUEST_1 == i) {
            TaskPointChooseRelativethingBean bean = JSON.parseObject(s, TaskPointChooseRelativethingBean.class);
            listBean = bean.getList();
            initPager();
        }
    }

    @Override
    public void onLoadError(String s) {

    }

    /**
     *  查询类目
     */
    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        /**
         * name		string	@mock=业
         query	分页数据	object
         page		number	@mock=1
         size		number	@mock=10
         sort		string	@mock=
         */
        loadData(TaskCenterConstant.URL_GET_CATEGORY_INFO, map, REQUEST_1);
    }

    /**
     * 保存父节点类目
     */
    public void saveData(){
        Map<String,Object> map = new HashMap<>();
        /**
         * categories	类目信息	array<object>
         name	类目名字	string	@mock=$order('name','name')
         ukid	类目ukid	number	@mock=$order(100010,100011)
         jobPointUkid	作业点ukid	number	@mock=100000110
         type	当前只写：类型：CATEGORY	string	CATEGORY
         */

        List<Map<String,Object>> list = new LinkedList<>();
        Map<String , Object> categoriesMap = new HashMap<>();

        TaskPointChooseRelativethingBean.ListBean listBean = null;
        Set<String> set = listBeanMap.keySet();
        for(String setKey : set){
            listBean = listBeanMap.get(setKey);
            categoriesMap.put("name",listBean.getCategoryName());
            categoriesMap.put("ukid",listBean.getMetaCategoryUkid());
            list.add(categoriesMap);
        }

        map.put("categories",list);
        map.put("jobPointUkid",jobPointUkid);
        map.put("type","CATEGORY");

        NoHttpUtils.httpPost(TaskCenterConstant.URL_ADD_JOBPOINT_STEP4_PARENT_SAVEITEMMES,map,myOnresponse,REQUEST_CODE);
        showProgressDialog();
    }

    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            if("0".equals(commonClass.getCode())){
                //// TODO: 2017/10/28   返回值的定制
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };

    @Override
    public void onClick(View v) {
        if (v == mBtSave) {
            DialogTools.getInstance().showCustomDialogPrompt(mActivity, "提示", "确认保存所选关联生产物吗？", false, new DialogTools.ConfirmListener() {
                @Override
                public void setConfirmListener(Dialog dialog, View view, String s) {
                    //确定
                    saveData();
                }
            }, "保存", "取消");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(TaskPointChooseRelativethingBean.ListBean bean) {

        if(listBeanMap.containsKey(bean.getMetaCategoryUkid())){ // 含有就删除 不含有添加
            TaskPointChooseRelativethingBean.ListBean inBean = listBeanMap.get(bean.getMetaCategoryUkid());
            listBeanMap.remove(inBean);
        }else{
            listBeanMap.put(""+ bean.getMetaCategoryUkid(),bean);
        }
    }
}